﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LimpaGaragem.DAO.Product
{
    public class Category
    {
        /// <summary>
        /// Return list of category
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.Product.Category> GetListOfCategory()
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR010_ListProductCategories", 0);

                List<LimpaGaragem.Model.Product.Category> objListOfCategory = new List<LimpaGaragem.Model.Product.Category>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.Product.Category objCategory = new LimpaGaragem.Model.Product.Category();
                    objCategory.ID = Convert.ToInt32(objDataRow["ID_CATEGORY"]);
                    if(objDataRow["ID_CATEGORY_PARENT"] != DBNull.Value)
                        objCategory.IDParent = Convert.ToInt32(objDataRow["ID_CATEGORY_PARENT"]);
                    objCategory.Name = objDataRow["NM_CATEGORY"].ToString();
                    objListOfCategory.Add(objCategory);
                }
                return objListOfCategory;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Return data of category
        /// </summary>
        /// <returns></returns>
        public LimpaGaragem.Model.Product.Category GetCategory(int IDCategory)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_CATEGORY", IDCategory));

                //RETURN DATA
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR010_SelectProductCategory", 0, objListOfParameters);

                LimpaGaragem.Model.Product.Category objCategory = null;
                if (objDataTable.Rows.Count > 0)
                {
                    objCategory = new LimpaGaragem.Model.Product.Category();
                    objCategory.ID = Convert.ToInt32(objDataTable.Rows[0]["ID_CATEGORY"]);
                    if (objDataTable.Rows[0]["ID_CATEGORY_PARENT"] != DBNull.Value)
                        objCategory.IDParent = Convert.ToInt32(objDataTable.Rows[0]["ID_CATEGORY_PARENT"]);
                    objCategory.Name = objDataTable.Rows[0]["NM_CATEGORY"].ToString();
                    
                }
                return objCategory;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
